package com.t.bricks.bussiness.service.shoppingcart.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.shoppingcart.ShoppingCartDao;
import com.t.bricks.bussiness.db.entity.shoppingcart.ShoppingCart;
import com.t.bricks.bussiness.db.entity.shoppingcart.WaresSmall;
import com.t.bricks.bussiness.db.entity.shoppingcart.base.ShoppingCartBase;
import com.t.bricks.bussiness.model.shoppingcart.ConditionShoppingCart;
import com.t.bricks.bussiness.service.shoppingcart.ShoppingCartService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 购物车'ShoppingCart表'基本业务操作类接口实现
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartDao shoppingCartDao;

	/**
	 * 新增
	 * @param shoppingCart 购物车 对象实体,注意:跨包访问时可能会是json字符串
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity add(Object shoppingCart) {
		MsgEmity me = ModuleUtil.objToEntity(shoppingCart, ShoppingCart.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		ShoppingCart shoppingCartData = me.getData();
		
		me = ModuleUtil.validDbEntityByAdd(shoppingCartData);// 通用的基本数据验证,如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		if (shoppingCartData.getiCount().intValue() < 1) {
			return MsgEmity.err(8001, "商品数量不能小于1");
		}
		
		String sUserId = ModuleUtil.currentLoginUserId();
		if (!sUserId.equals(shoppingCartData.getsCustomerId())) {
			return MsgEmity.err(8002, "顾客账号与当前用户不一致");
		}
		
		me = shoppingCartDao.findWares(shoppingCartData.getsWaresId());//查找商品信息
		if (!me.isSuccess()) {
			return MsgEmity.err(50);
		}
		
		Date now = new Date();
		
		WaresSmall waresSmall = me.getData();
		if (waresSmall.getdStart().after(now)) {
			return MsgEmity.err(8003, "商品未上架");
		}
		
		if (waresSmall.getdStop().before(now)) {
			return MsgEmity.err(8004, "商品已下架");
		}
		
		if (waresSmall.getiMaxBuy().intValue() < shoppingCartData.getiCount()) {
			return MsgEmity.err(8005, "超过商品最大购买量");
		}
		
		//查询商品购物车数据
		me = shoppingCartDao.findByWaresId(shoppingCartData.getsCustomerId(), shoppingCartData.getsWaresId());
		if (!me.isSuccess()) {
			return me.appendDataCode(1100);
		}
		
		//-- 如果已经有记录,并且旧数据没有过期,则累加记录中的数量 --//
		ShoppingCart oldData = me.getData();
		if (oldData != null && oldData.getiState().intValue() == 1) {
			int iNewCount = oldData.getiCount() + shoppingCartData.getiCount();
			if (waresSmall.getiMaxBuy().intValue() < iNewCount) {
				return MsgEmity.err(8006, "加购后超过商品最大购买量");
			}
			
			me = shoppingCartDao.editCount(oldData.getuId(), oldData.getiVersion(), shoppingCartData.getiCount());
			if (!me.isSuccess()) {
				return me;
			}
			
			oldData.setiVersion(oldData.getiVersion() + 1);//版本号改变
			oldData.setiCount(iNewCount);//数量改变
			return MsgEmity.success(oldData, "商品加购成功");
		}
		
		if (waresSmall.getiMinBuy().intValue() > shoppingCartData.getiCount()) {
			return MsgEmity.err(8007, "商品最小购买量是:", String.valueOf(waresSmall.getiMinBuy().intValue()));
		}
		
		//-- 没有数据或者旧数据为过期数据,则新增 --//
		me = ModuleUtil.validDbEntityRepeatByAdd((ShoppingCartBase)shoppingCartData, shoppingCartDao);//查询数据库,验证数据是否有重复,因验证数据重复的方式比较复杂,直接在sql脚本中处理
		if (!me.isSuccess()) {// 在false下,如果返回8028则表示数据库查询异常,否则返回后4位是8026的数据,如xxx8026,从万位起表示从数据库中查询的结果
			String tmp = me.getData().toString();
			tmp = tmp.substring(tmp.length() -4, tmp.length());// 数据一定不小于4位
			return me.setData(tmp);// 发现重复数据后不进行其它业务处理则退出时,必须清理返回数据
		}
		
		shoppingCartData.setuId(null);//清空记录编号
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(shoppingCartData);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}
		
		return shoppingCartDao.add(shoppingCartData);
	}

	/**
	 * 删除
	 * @param iVersion 数据版本号
	 * @param uId 记录编号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(Integer iVersion, String uId) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "信息编号参数错误，请刷新重试！", 8001);
		}
		
		if (null == iVersion || iVersion.intValue() < 1) {
			return new MsgEmity(false, "信息版本参数错误，其值应大于0！", 8002);
		}

		return shoppingCartDao.del(uId, iVersion);
	}
	
	/**
	 * 查询分页
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionShoppingCart condition) {
		if (null == page) {
			page = new Page(new ArrayList<ShoppingCart>());
		}

		return shoppingCartDao.findByPage(page, orders, sLikeStr, condition);
	}

	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param uId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String uId) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return shoppingCartDao.findByPageRow(uId);
	}

	/**
	 * 修改数量
	 * @param uId 购物车记录
	 * @param iVersion 当前版本号
	 * @param iAppendCount 增加(减少)的数量
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity editCount(String uId, Integer iVersion, Integer iAppendCount) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		if (null == iVersion || iVersion.intValue() < 1) {
			return new MsgEmity(false, "信息版本参数错误，其值应大于0！", 8002);
		}
		
		if (null == iAppendCount) {
			return new MsgEmity(false, "改动值参数缺失！", 8003);
		}
		
		MsgEmity me = shoppingCartDao.editCount(uId, iVersion, iAppendCount);
		if (!me.isSuccess()) {
			return me;
		}
		
		me = shoppingCartDao.findByPageRow(uId);
		if (!me.isSuccess()) {
			return me.appendDataCode(1100);
		}
		
		return me.setMsg("修改数量成功,并返回最新结果");
	}

	/**
	 * 批量删除数据
	 * @param ids 记录编号串(逗号分隔)
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity dels(String ids) {
		if (null == ids || "".equals(ids.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		String[] array = ids.split(",");
		List<String> list = new ArrayList<>(array.length);
		for (String string : array) {
			list.add(string.trim());
		}
		
		return shoppingCartDao.dels(list);
	}
	
}
